import os
import shutil


def move_file(srcfile, dstfile):
    if not os.path.isfile(srcfile):
        print("%s not exist!"%(srcfile))
    else:
        fpath, fname=os.path.split(dstfile)    #分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)                #创建路径
        shutil.move(srcfile, dstfile)          #移动文件
        print("move %s -> %s"%(srcfile,dstfile))


def rename_stock_file():
    data_path = "E:\\MarketData\\Stock\\"
    os.chdir(data_path)
    all_folder = os.listdir(data_path)
    for folder_name in all_folder:
        folder_path = f"{data_path}{folder_name}\\"
        os.chdir(folder_path)
        all_files = os.listdir(folder_path)
        for file_name in all_files:
            stock_id, exchange, suffix = file_name.split("_")
            new_file_name = f"{stock_id}_{suffix}"
            os.rename(file_name, new_file_name)
            print("rename %s -> %s" % (file_name, new_file_name))


def rename_jsl_file():
    data_path = "E:\\QMT\\jsl\\"
    os.chdir(data_path)
    all_files = os.listdir(data_path)
    for file_name in all_files:
        new_file_name = file_name.replace("-","")
        os.rename(file_name, new_file_name)
        print("rename %s -> %s" % (file_name, new_file_name))


if __name__ == '__main__':
    """
    data_path = "E:\\MarketData\\ConvertibleBond\\"
    os.chdir(data_path)
    all_files = os.listdir(data_path)
    for file_name in all_files:
        if os.path.isfile(file_name):
            file_id, _, suffix = file_name.split("_")
            dst_file = f"{file_id}\\{file_id}_{suffix}" # 文件明去掉中间的交易所标志
            move_file(file_name, dst_file)
    """
    #rename_stock_file()
    rename_jsl_file()
